<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\plan\controller\api;
use app\common\controller\Api;
use think\response\Json;

/**
 * 预案管理
 * @package app\plan
 * @author LJQ<245369209@qq.com>
 */
class Plan extends Api

{

    /**
   *  预案列表
   * @param string 
   * @return Json
   */

	public function planList($scope = null,$planId=null){
              $referer=$_SERVER['HTTP_REFERER'];
             
              $where= [] ;
              if(strpos($referer, 'company')!==false){
                  $with='scripts,enterprise,scripts.staff';
                  $where['formId']=1;
                  $where['natureId']=1;
                  if ($scope) {
                     $where['unitId']=['in',$scope]; 
                  }

				
                  if ($planId) {

                      $where['id']=['eq',$planId]; 
                  }
             }

              if(strpos($referer, 'government')!==false){
                  $with='scripts,unit';
                  $where['formId']=2;
                  $where['natureId']=1;

                   if ($planId) {
                      $where['id']=['eq',$planId]; 
                  }
              }
		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			// 预案列表
			$info =collection(model('plan')->with($with)->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//预案列表
			$info =collection(model('plan')->with($with)->where($where)->select())->toArray();
		}
	   	 
	 	//总数
	   	 $count = model('plan')->where($where)->count('id'); 
		
    		   
         return json(['code'=>1,'msg'=>'预案','count'=>$count,'data'=>$info]);
}
   	  
   	      //添加预案
   	 public function add($data){
          
          if (empty($data)) {
              return json(['code'=>0,'msg'=>'数据异常,请检查']);
          }
          $model = model('plan/plan');
          $result = $model->change($data);
          if (false != $result) {
             setlog('planadd','plan',session('user_auth.id'));

              return json(['code'=>1,'msg'=>'预案添加成功']);
          } else {
              return json(['code'=>0,'msg'=>'预案添加失败,请检查']);
          }
      }

   	

   /**
   * 修改预案
   * @param  string planId 预案id,
   * @return Json
   */
   	public function edit($data,$planId){
        
          if (empty($planId)) {
              return json(['code'=>0,'msg'=>'      预案不存在,请检查']);
          }
          $model = model('plan/plan');     
          if ($model->change($data)) {
            setlog('planupdate','plan',session('user_auth.id'));
             return json(['code'=>1,'msg'=>'预案修改成功']);
            }else{
             return json(['code'=>0,'msg'=>'预案预案修改失败,请检查']);
            }
    }
    /**
   *  删除预案
   * @param string planId 预案id,
   * @param Json
   */
     
    public function del($planId) {
        $planId = input('planId',0,'intval');
        $model = model('plan/plan');
      	if (empty ( $planId )||empty(($model->where(array('id'=>$planId))->find()))) {
           	return  json(['code'=>0,'msg'=>'预案id不存在,请检查']);
        }
        if($model->destroy(['id'=>$planId]) !== FALSE){
           setlog('plandel','plan',session('user_auth.id'));
        	 return json(['code'=>1,'msg'=>'预案删除成功']);
       }else{
         	 return $this->json(['code'=>0,'msg'=>'预案删除失败,请检查']);
       }
    }

     
   /**
   *   查询预案
   * @param keyword预案名称
   * @return Json
   */
   	public function search($typeId=null,$unitId=null){
          $referer=$_SERVER['HTTP_REFERER'];
          $where= [] ;
  		
      if (strpos($referer, 'company')!==false) {
                  $with='scripts,enterprise,scripts.staff';
                  $where['natureId']=1;
                  $where['formId']=1;
                if($typeId){
      
                     $where['typeId'] = ['eq',$typeId];
                 }
                if($unitId){
      
                     $where['unitId'] = ['eq',$unitId];
                }
          }

           if (strpos($referer, 'government')!==false) {
                   $with='scripts,unit';
                   $where['natureId']=1;
                   $where['formId']=2;
                if($typeId){
                      $where['typeId'] = ['eq',$typeId];
                   }
                if($unitId){
                         $where['unitId'] = ['eq',$unitId];
                    }
           }

   		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			//预案列表
			$info =collection(model('plan')->with($with)->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//预案列表
			$info =collection(model('plan')->with($with)->where($where)->select())->toArray();
		}
	 	//总数
	       $count = model('plan')->where($where)->count('id'); 
        
		 
    	       if (empty($info) && $count == 0) {
              	return json(['code'=>0,'msg'=>'没有找到预案','count'=>$count]);
       	} 
       	
       	if ($count > 0){
       		return json(['code'=>1,'msg'=>'预案','count'=>$count,'data'=>$info]);
       	}
      } 

      
      /**
       * 申请审核
       * @param string $hazardId
       */
      public function audit($planId = null){
      	$referer = $_SERVER['HTTP_REFERER'];
      	if(strpos($referer, 'company')!==false){
      		$wfType = 5;
      	}elseif(strpos($referer,'government') !== false){
      		$wfType = 4;
      	}
      	$workflow = model('Workflow')->where('wfType',$wfType)->find();
      	if(empty($workflow)||empty($workflow->steps)){
      		return json(['code'=>0,'msg'=>'无对应审核流程','count'=>'','data'=>'']);
      	}else{
      		$plan = model('Plan')->get($planId);
      		if($plan->isUpdate(true)->save(['checkStatus'=>2]) !== false){
      			$data['title'] = $plan['title'];
      			$data['wfFlowInfoId'] = $workflow['id'];
      			$data['wfType'] = $wfType;
      			$data['busiId'] = $planId;
      			if($plan['formId'] == 1){
      				$data['enterpriseId'] = $data['unitId'];
      			}
      			$data['applyPerson'] = session('user_auth.id');
      			if(model('Wfitem')->save($data)){
      				return json(['code'=>1,'msg'=>'申请成功','count'=>'','data'=>'']);
      			}else{
      				return json(['code'=>0,'msg'=>'申请失败','count'=>'','data'=>'']);
      			}
      		}
      	}
      	return json(['code'=>0,'msg'=>'申请失败','count'=>'','data'=>'']);
      	 
      }

      
}